﻿
namespace RFIDStation.DeviceDriver
{
    public class Worker : BackgroundService
    {
        private readonly ILogger _logger;
        private readonly IEnumerable<IModules.IModule> _moduleServices;

        public Worker( ILoggerFactory loggerFactory, IEnumerable<IModules.IModule> moduleServices )
        {
            _logger = loggerFactory.CreateLogger( nameof( Worker ) );
            _moduleServices = moduleServices;
        }

        protected override async Task ExecuteAsync( CancellationToken stoppingToken )
        {
            foreach ( var module in _moduleServices )
            {
                await module.StartAsync( stoppingToken );
            }

            _logger.Info( $"Worker running at: {DateTimeOffset.Now}" );
        }

        public override async Task StopAsync( CancellationToken cancellationToken )
        {
            foreach ( var module in _moduleServices )
            {
                await module.StopAsync( cancellationToken );
            }

            _logger.Info( $"Worker stop at: {DateTimeOffset.Now}" );
        }
    }
}
